#include "Thread.hpp"

namespace se {

Thread::Thread(ThreadCallBack &&callBack) : _threadId(0), _callBack(std::move(callBack)) { }

Thread::Thread(const ThreadCallBack &callBack) : _threadId(0), _callBack(callBack) { }

void Thread::start() {
    ::pthread_create(&this->_threadId, nullptr, Thread::threadFunction, this);
}

void Thread::join() {
    if (this->_threadId) {
        ::pthread_join(this->_threadId, nullptr);
    }
}

void *Thread::threadFunction(void *argument) {
    if (argument) {
        static_cast<Thread *>(argument)->_callBack();
    }
    ::pthread_exit(nullptr);
}

}  // namespace se